package com.example.projectjava.dao;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

//针对appUser表做SQL操作的类
@Repository   //此注解的作用：与控制器中的@Controller注解的功能相同，意为将该类纳入Spring容器管理
public class ProductDao {
    @Autowired
    JdbcTemplate jdbcTemplate;
   //增
    public  int productadd(int product_id,int category_id,String product_name,double price,
                           int stock){
        String sql = "insert into product (product_id,category_id,product_name,price,stock) values(?,?,?,?,?)";
        return    jdbcTemplate.update(sql,product_id,category_id,product_name,price,stock);//返回了一个整数，用来表示此操作有没有成功
    }
    //删
    public int productdelete(int product_id){
        String sql  = "DELETE from product where product_id=?";
        return  jdbcTemplate.update(sql,product_id);
    }
    //改
    public  int  productupdate(String product_id,String category_id,String product_name,String price,String stock){
        String sql = "update product set category_id=?,product_name=?,price=?,stock=? where product_id=?";
        return    jdbcTemplate.update(sql,category_id,product_name,price,stock,product_id);
    }
    //查
    public  List<Map<String, Object>>  productquery(int product_id){
        String sql = "select * from product where product_id=?";
        return  jdbcTemplate.queryForList(sql,product_id);
    }
    public List<Map<String,Object>> queryAllproduct(){
        String sql = "SELECT product_id,category_id,product_name,price,stock  FROM `product` ";
        return  jdbcTemplate.queryForList(sql);
    }
}
